#!/bin/sh
# This script can start and stop daemon that monitoriza the internet link status
#
### BEGIN INIT INFO
# Provides:          inetlink-monitor-daemon
# Required-Start:    $syslog $time
# Required-Stop:     $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Monitorize el internet link status
# Description:       This script allows start and stop the program that monitorize 
#                    every 30 seconds if out internet conection is up or down.
### END INIT INFO


# ---------------------------------------------------------
#   Load and check the configuration
# ---------------------------------------------------------

 # Load config file
 ConfigFile=/usr/local/TECNOQUIA/etc/dynIP.cfg
 if [ ! -e $ConfigFile ] || [ ! -r $ConfigFile ]
 then
    echo "ERROR: I can't read the config file [$ConfigFile]" >&2
    exit 5
 fi
 . $ConfigFile
    
  
 # Check the directory that i must save the log files
 BaseDir=$DIRECTORY_LOG
 mkdir -m 777 "$BaseDir" -p 2>/dev/null
 if [ ! -w $BaseDir ]
 then
    echo "ERROR: I can't write into [$BaseDir]. Check that exists and the permissions on this directory" >&2
    exit 6
 fi

 # Load debian daemons function...
 . /lib/lsb/init-functions

 # Wich command we must launch
 runCMD=/usr/local/TECNOQUIA/bin/internet_link_monitor
 # Wich user run this command 
 runAS=nobody
 # Get script name
 nameCMD=`basename $runCMD`
 # Get daemon name
 nameDAEMON=`basename $0`
 # Where is the log file
 logFile=$BaseDir/internet_link_status.log
 # Store pid into file
 pidFile=/var/run/$nameCMD.pid

 if [ ! -f $runCMD ]
 then
    echo "ERROR: The script [$runCMD] does not exists!" >&2
    exit 5
 fi


# ---------------------------------------------------------
#   Functions
# ---------------------------------------------------------

# STATUS ##################################################
status () {
   # Know how many proccess there are in execution
   pidList=`ps -eo pid,cmd | grep "$runCMD" | grep -v ' grep' | grep -v ' su ' | awk '{printf("%s ",$1)}' `
   if [ "$pidList" = "" ]
   then
      # Uhhm... there seems no Pids
      if [ -e $pidFile ] 
      then
         echo -e "\nIt is stopped, but exists PID file" 
      else
         echo -e "\nthe daemon is stopped" 
      fi
      return 3
   else
      echo -e "\nthe daemon is running with the PID [$pidList] ... " 
   fi
   return 0
}

# START ###################################################
start () {
   status >/dev/null 
   if [ $? -eq 3 ]
   then 
      touch $pidFile $logFile
      chown $runAS $pidFile $logFile
      /bin/su -s /bin/bash - $runAS -c "export HOME=$BaseDir && /usr/bin/nohup $runCMD </dev/null >/dev/null 2>&1  & " 2>/dev/null >/dev/null
   else
      echo -e "\nthe daemon is running" 
   fi
   return 0
}

# STOP ####################################################
stop () {
   status >/dev/null 
   if [ $? -eq 0 ]
   then 
      procPid=`cat $pidFile 2>/dev/null`
      [ "$procPid" != "" ] && kill -9 $procPid 
      pidList=`ps -eo pid,cmd | grep "$runCMD" | grep -v ' grep' | awk '{printf("%s ",$1)}' `
      [ "$pidList" != "" ] && kill -9 $pidList
      rm -f $pidFile 2>/dev/null
   else
      echo -e "\nthe daemon is stopped" 
   fi
   return 0
}



# ---------------------------------------------------------
#   M A I N
# ---------------------------------------------------------

retVal=0
case "$1" in
start)	log_daemon_msg "Starting internet link monitor" "$nameCMD"
        start 
	;;
stop)	log_daemon_msg "Stopping internet link monitor" "$nameCMD"
        stop
        ;;
status)	status
        retVal=$?
        ;;
restart) log_daemon_msg "Restarting internet link monitor" "$nameCMD"
	stop
        start
        ;;
*)	log_action_msg "Use: /etc/init.d/$nameDAEMON {start|stop|restart|status}"
        exit 2
        ;;
esac

# return value exit
test $retVal -eq 0
retVal=$?

log_end_msg $retVal
exit $retVal 

